/*----------------------------------------------------------------------
 *
 * Copyright 2009, Thomas Dejanovic.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 *
 *---------------------------------------------------------------------
 *
 * Serial port interface to simple APB bus.
 *
 * based on code on the Leon / Rachel SPARC project.
 *
 *---------------------------------------------------------------------*/


module dcom_apb

  (/*AUTOARG*/
  // Outputs
  rxd_out, pclk, pclk_en, gated_pclk, gated_pclk_en, preset_l,
  penable, psel, paddr, pwrite, pwdata,
  // Inputs
  clk, clk_en, reset, txd_in, dcom_scaler, prdata
  );

  //----------------------------------------
  input                 clk;
  input                 clk_en;

  input                 reset;
  //----------------------------------------

  //----------------------------------------
  // serial port interface.
  input                 txd_in;
  output                rxd_out;

  // scaler value is supplied externally
  // values are clock rate / (8 x baud rate) - 1, so for:
  // 115200 @ 100MHz = 108
  //  57600 @ 100MHz = 216
  // 115200 @  90MHz =  97
  //  57600 @  90MHz = 194
  // 115200 @  50MHz =  53
  //  57600 @  50MHz = 108
  input [7:0]           dcom_scaler;
  //----------------------------------------

  //-----------------------------------------
  // apb interface.
  output                pclk;
  output                pclk_en;
  output                gated_pclk;
  output                gated_pclk_en;

  output                preset_l;
  output                penable;
  output                psel;
  output [29:0]         paddr;
  output                pwrite;
  output [31:0]         pwdata;
  input [31:0]          prdata;
  //-----------------------------------------


  /*----------------------------------------------------------------*/

  /*-AUTOUNUSED-*/

  /*AUTOINPUT*/

  /*AUTOOUTPUT*/

  /*-AUTOREGINPUT-*/

  /*AUTOREG*/

  /*AUTOWIRE*/
  // Beginning of automatic wires (for undeclared instantiated-module outputs)
  wire [7:0]            rx_data;                // From uart of dcom_uart.v
  wire                  rx_data_valid;          // From uart of dcom_uart.v
  wire                  rx_error;               // From uart of dcom_uart.v
  wire [7:0]            tx_data;                // From apb_if of dcom_apb_if.v
  wire                  tx_data_valid;          // From apb_if of dcom_apb_if.v
  wire                  tx_ready;               // From uart of dcom_uart.v
  // End of automatics

  /*------------------------------------------------------------------
   *
   * local definitions and connections.
   *
   * */

  /*-----------------------------------------------------------------
   *
   *
   *
   * */

  dcom_uart uart
    (/*AUTOINST*/
     // Outputs
     .rxd_out                           (rxd_out),
     .rx_data                           (rx_data[7:0]),
     .rx_data_valid                     (rx_data_valid),
     .rx_error                          (rx_error),
     .tx_ready                          (tx_ready),
     // Inputs
     .clk                               (clk),
     .clk_en                            (clk_en),
     .reset                             (reset),
     .txd_in                            (txd_in),
     .dcom_scaler                       (dcom_scaler[7:0]),
     .tx_data                           (tx_data[7:0]),
     .tx_data_valid                     (tx_data_valid));

  dcom_apb_if apb_if
    (/*AUTOINST*/
     // Outputs
     .tx_data                           (tx_data[7:0]),
     .tx_data_valid                     (tx_data_valid),
     .pclk                              (pclk),
     .pclk_en                           (pclk_en),
     .gated_pclk                        (gated_pclk),
     .gated_pclk_en                     (gated_pclk_en),
     .preset_l                          (preset_l),
     .penable                           (penable),
     .psel                              (psel),
     .paddr                             (paddr[29:0]),
     .pwrite                            (pwrite),
     .pwdata                            (pwdata[31:0]),
     // Inputs
     .clk                               (clk),
     .clk_en                            (clk_en),
     .reset                             (reset),
     .rx_data                           (rx_data[7:0]),
     .rx_data_valid                     (rx_data_valid),
     .rx_error                          (rx_error),
     .tx_ready                          (tx_ready),
     .prdata                            (prdata[31:0]));

  /*----------------------------------------------------------------*/

endmodule // dcom_apb


// Local Variables:
// verilog-library-directories:("." "../dcom_uart")
// verilog-library-extensions:(".v")
// End:
